package com.blb.service.admin.impl;

import com.blb.dao.admin.ProductDao;
import com.blb.dao.admin.impl.ProductDaoImpl;
import com.blb.pojo.Product;
import com.blb.pojo.Page;
import com.blb.service.admin.IProductService;
import com.blb.utils.ApplicationContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements IProductService {

    @Autowired
    private ProductDao productDao;

    @Override
    public Page<Product> page(int page, int size, String name) throws Exception {
        List<Product> list = productDao.selectPageByName(page, size, name);
        int count = productDao.count(name);
        return new Page<Product>(page, size, list, count);
    }

    @Override
    public void add(Product product) throws Exception {
        Product product1 = productDao.selectByName(product.getName());
        if(product1!=null){
            throw new Exception("商品名称已存在，请改正");
        }
        productDao.insert(product);
    }

    @Override
    public void deleteById(int id) throws Exception {
        productDao.deleteById(id);
    }

    @Override
    public void batchDelete(String[] ids) throws Exception {
        productDao.batchDelete(ids);
    }

    @Override
    public Product getById(int id) throws Exception {
        return productDao.selectById(id);
    }

    @Override
    public void update(Product product) throws Exception {
        Product product1 = productDao.selectByName(product.getName());
        if(product1!=null && product1.getId()!=product.getId()){
            throw new Exception("商品名称已存在，不能重复");
        }
        productDao.update(product);
    }

    @Override
    public void changeStatus(int id, int status) throws Exception {
        productDao.updateStatusById(id, status);
    }
}
